//
//  Hot_collectionViewCell.m
//  EggSteward
//
//  Created by 王道林 on 16/8/31.
//  Copyright © 2016年 王道林. All rights reserved.
//

#import "Hot_collectionViewCell.h"
#import "BaseTableView.h"
#import "Masonry.h"
#import "Hot_TableViewCell.h"
#import "MJRefresh.h"
#import "DLWNetworkingParser.h"
@interface Hot_collectionViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) BaseTableView *tableView;

// 记录刷新
@property (nonatomic, assign) NSInteger countOne;
@property (nonatomic, assign) NSInteger countTwo;
@property (nonatomic, assign) NSInteger countThree;


@end

@implementation Hot_collectionViewCell

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubView];
    }
    return self;
}

- (void)createSubView
{
    self.countOne = 1;
    self.countTwo = 1;
    self.countThree = 1;
    _tableView = [[BaseTableView alloc] initWithFrame:self.contentView.bounds style:UITableViewStylePlain];
//    _tableView.contentInset = UIEdgeInsetsMake(0, 0,49, 0);
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.contentView addSubview:_tableView];
    [_tableView registerClass:[Hot_TableViewCell class] forCellReuseIdentifier:@"reuse"];
    
    // 刷新
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter  footerWithRefreshingTarget:self refreshingAction:@selector(handleMore)];
}

- (void)handleMore
{
    if (self.cellNumber == 0) {
        self.countOne++;
        NSString *url = [NSString stringWithFormat:@"http://www.danjiguanjia.com/api/News/getHeadlines?page=%ld", self.countOne];
        [DLWNetworkingParser  POSTWithURL:url par:nil success:^(id responseObject) {
            for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
                Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
                [_arrData addObject:model];
            }
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
        [_tableView.mj_footer endRefreshing];
    }else if (self.cellNumber == 1)
    {
        self.countTwo++;
        NSString *url = [NSString stringWithFormat:@"http://www.danjiguanjia.com/api/News/getDailymicro?page=%ld", self.countTwo];
        [DLWNetworkingParser  POSTWithURL:url par:nil success:^(id responseObject) {
            for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
                Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
                [_arrData addObject:model];
            }
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
        [_tableView.mj_footer endRefreshing];
        
    } else
    {
        self.countThree++;
        NSString *url = [NSString stringWithFormat:@"http://www.danjiguanjia.com/api/News/getDisease?page=%ld", self.countThree];
        [DLWNetworkingParser  POSTWithURL:url par:nil success:^(id responseObject) {
            for (NSDictionary *dic in responseObject[@"ticket"][@"data"]) {
                Home_NewsModel *model = [Home_NewsModel modelWithDic:dic];
                [_arrData addObject:model];
            }
            [_tableView reloadData];
        } filed:^(NSError *error) {
            
        }];
        [_tableView.mj_footer endRefreshing];
    }
}


//布局
- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
    [super applyLayoutAttributes:layoutAttributes];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentView.mas_left);
        make.right.mas_equalTo(self.contentView.mas_right);
        make.top.mas_equalTo(self.contentView.mas_top);
        make.bottom.mas_equalTo(self.contentView.mas_bottom);
    }];
}
#pragma mark - tableview的协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arrData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    Hot_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse" forIndexPath:indexPath];
    cell.model = _arrData[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)setArrData:(NSMutableArray *)arrData
{
    _arrData = arrData;
    [_tableView reloadData];
}

#pragma mark - tableViewCell 的点击
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Home_NewsModel *model = _arrData[indexPath.row];
    self.blockModel(model);
}


@end
